package com.example.testcomposeproject.pages.testChart

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.ui.theme.AppTheme

import android.graphics.Color
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc:
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestPieChartUI(modifier: Modifier = Modifier) {
    AppTheme {
        PieChartView()

    }
}






@Composable
fun PieChartView() {
    AndroidView(
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp)
            .padding(16.dp),
        factory = { context ->
            PieChart(context).apply {
                description.isEnabled = false
                legend.isEnabled = false
                // 设置饼图数据
                val entries = listOf(
                    PieEntry(20f, "Category A"),
                    PieEntry(30f, "Category B"),
                    PieEntry(50f, "Category C")
                )
                val dataSet = PieDataSet(entries, "Pie Chart").apply {
                    colors = listOf(
                        Color.RED,
                        Color.GREEN,
                        Color.BLUE
                    )
                }
                data = PieData(dataSet)
            }
        },
        update = { pieChart ->
            // 在这里更新饼图数据
            pieChart.invalidate() // 刷新图表
        }
    )
}